OPTIONS LINESIZE=80;
TITLE 'example';

libname libS 'C:\Documents and Settings\yoav\My Documents\school\work\AMP\ampForm\results';
libname libB 'C:\Documents and Settings\yoav\My Documents\school\BigFiles\ampass';
libname libBB 'C:\Documents and Settings\yoav\My Documents\school\BigFiles\ampform';

libname libS 'C:\Documents and Settings\user1\My Documents\My Dropbox\work\research\att\measure\AMP\amp.studies\ampForm\results';
libname libB 'C:\Documents and Settings\user1\My Documents\bigfiles\ampass';
libname libBB 'C:\Documents and Settings\user1\My Documents\bigfiles\ampform';

/*The data was accidently saved under the name of a previous study; so not all data are relevant*/
data ds; set libB.iata1000000; where task_name_s = "amprv"; run;

proc freq data=ds; tables TRIAL_NAME_S; run;
DATA amp; SET ds;

	prime = '123456789123456789'; prm = '1234';

	prime = substr(TRIAL_NAME_S, index(TRIAL_NAME_S, "[")+1, 
			index(TRIAL_NAME_S, ".") - index(TRIAL_NAME_S, "[") - 1);
	*set cond;
	if (index(prime, "raigh") > 0) then prm = "vab";
	else if (index(prime, "urly") > 0) then prm = "rml";
	else if (index(prime, "Grey") > 0) then prm = "gry";
	else prm = "none";

	*set reponse;
	if TRIAL_RESPONSE_S eq "Pleasant" then plsnt = 1; 
	else if TRIAL_RESPONSE_S eq "Unpleasant" then plsnt = 0; 

	subject = SESSION_ID;
run;
proc freq data=amp; tables prm; run;

/*mean of resp for each prime*/
PROC MEANS DATA=amp n MEAN MAXDEC=2 std stderr stddev;VAR plsnt;CLASS prm;RUN;
proc sort data=amp; by subject prm; run;
proc means noprint data=amp; by subject prm; var plsnt;output out=means mean=plsnt;run;
/*proc print data=means;run;*/
proc transpose data=means name=name out=respds prefix=r; id prm; by subject; run;
proc print data=respds;run;
data nds; set respds; if name="_FREQ_"; nnone=rnone; nvab=rvab; nrml=rrml; ngrey=rgry;
minn = min(nvab, nrml, ngrey); minvr = min(nvab, nrml);
keep subject nnone nvab nrml ngrey minn minvr; run;
proc print data=nds;run;
data libBB.nds; set nds; run;

proc means noprint data=amp; by subject prm; var plsnt; output out=mns1 mean=plsnt; 
where block_number=1; run;
proc transpose data=mns1 name=name out=respds1 prefix=r1; id prm; by subject; run;
proc means noprint data=amp; by subject prm; var plsnt; output out=mns2 mean=plsnt; 
where block_number=2;run;
proc transpose data=mns2 name=name out=respds2 prefix=r2; id prm; by subject; run;
proc print data=respds2;run;
data respds1; set respds1; if name="plsnt";keep subject r1vab r1rml r1gry; run;
data respds2; set respds2; if name="plsnt";keep subject r2vab r2rml r2gry; run;
data respds; set respds; if name="plsnt";keep subject rnone rvab rrml rgry; run;

PROC SORT DATA=respds; BY subject; RUN;
PROC SORT DATA=respds1; BY subject; RUN;
PROC SORT DATA=respds2; BY subject; RUN;

data theamp; merge respds respds1 respds2; by subject;run;
proc print data=theamp;run;
data libBB.theamp; set theamp; run;

/*READ EXPLICIT*/
data eds; set libB.explicita1000000;run;
proc freq data=eds; tables QUESTIONNAIRE_NAME_S; run;
data eExp; set eds;
where index(QUESTIONNAIRE_NAME_S,"exp") > 0 or index(QUESTIONNAIRE_NAME_S,"lng") > 0;
subject=SESSION_ID; 
expR = INPUT(QUESTION_RESPONSE_S, 4.0); 
qst = QUESTION_NAME_S;
qstN = QUESTION_NUMBER;
keep subject TASK_NUMBER SESSION_ID qst qstN expR; run;
proc freq data=eExp; tables qst; run;

proc sort data=eExp;by subject;run;
PROC TRANSPOSE DATA=eExp OUT=qds;
VAR expR;BY subject;ID qst;RUN;
data qds; set qds; drop _NAME_;run;
proc contents data=qds;run;
  
PROC MEANS DATA=qds fw=7 MEAN MAXDEC=2 n range median min max std stderr;
VAR ereem evabb engotr dstrct learn   cntrl   intent   seeF   read   infl;RUN;
data qds; set qds;ermlvab = ereem-evabb;run;
PROC MEANS DATA=qds fw=7 MEAN MAXDEC=2 n range median min max std stderr;VAR ermlvab;RUN;
data libBB.qds; set qds; run;

*****************************;
*add data from registration;
data demo; set libB.demoa; run;
proc sort data=demo; by USER_ID;run;
proc transpose data=demo name=name out=demoDS; by USER_ID; var VALUE_S;id CHARACTERISTIC_S;
proc print data=demoDS;run;
proc freq data=demoDS; tables relfamily; run;
proc freq data=demoDS; tables religion; run;
data demoDS; set demoDS; 
format dPlt 2.;
dPlt = politicalid;
age = 2007-input(birthyear,4.0);
orgn = citizenship;
race='.....';
if raceomb=1 then race='ntvAm';
if raceomb=2 then race='eAsia';
if raceomb=3 then race='sAsia';
if raceomb=4 then race='natPc';
if raceomb=5 then race='black';
if raceomb=6 then race='white';
if raceomb=7 then race='vabwh';
if raceomb=8 then race='mixed';
if raceomb=9 then race='unkno';
sex = substr(sex, 1,1);
keep dPlt sex age orgn race USER_ID;run;
*merge with session data;
data ssds; set libB.sessiontaska1000000; run;
PROC SORT DATA=ssds; BY SESSION_ID; RUN;
PROC TRANSPOSE DATA=ssds OUT=tss PREFIX=s;VAR USER_ID;BY SESSION_ID; RUN;
data tss; set tss; USER_ID = s1; keep SESSION_ID USER_ID; run;
PROC SORT DATA=demoDS; BY USER_ID; RUN;
PROC SORT DATA=tss; BY USER_ID; RUN;
data demoDS; merge demoDS tss; by USER_ID; drop _NAME_; run;
data demoDS; set demoDS; where SESSION_ID > 0; subject = SESSION_ID; run;
data libBB.demoDS; set demoDS;run;

*****************;
*read tasks order;
data ssds; set ssds; subject = SESSION_ID;run;
PROC SORT DATA=ssds; BY subject TASK_ID_S;RUN;
PROC UNIVARIATE DATA=ssds NOPRINT;
VAR TASK_NUMBER;OUTPUT OUT=usession mean=mNum mode=tNum;BY subject TASK_ID_S;RUN;
PROC TRANSPOSE DATA=usession OUT=orderDS prefix=ord;VAR TASK_ID_S;ID tNum;BY subject;RUN;
proc freq data=orderDS; tables ord6; where ord2="intro1"; run;
data orderDS; set orderDS;
if ord2 = "intro1"; ampform=1;
formcnd="XXX"; formcnd = ord5;
if index(ord6,"exp") > 0 then order = "ea"; 
else if index(ord6,"Amp") > 0 then order = "ae"; 
run;
proc freq data=orderDS; tables formcnd; run;
data libBB.orderDS; set orderDS; run;

*If needed: read all data from lib;
data nds; set libBB.nds; run;
data theamp; set libBB.theamp; run;
data qds; set libBB.qds; run;
data demoDS; set libBB.demoDS; run;
data orderDS; set libBB.orderDS; run;
*merge with all data;
PROC SORT DATA=nds; BY subject; RUN;
PROC SORT DATA=theamp; BY subject; RUN;
PROC SORT DATA=qds; BY subject; RUN;
PROC SORT DATA=demoDS; BY subject; RUN;
PROC SORT DATA=orderDS; BY subject; RUN;

data allds; run;
data allds; merge nds theamp qds demoDS orderDS; by subject; run;
data allds;set allds;if ampform=1;run;

data allds;set allds;if ampform=1;
/*amp scores*/
amp = rrml - rvab; amp1 = r1rml-r1vab;amp2 = r2rml-r2vab;
amprml = rrml-rgry; ampvab = rvab-rgry;
/*absolute*/
aamp = abs(amp); aamprml = abs(amprml); aampvab = abs(ampvab);
amp_extrm = mean(aamprml, aampvab); SDAmp = std(rrml, rvab, rgry);
/*truncate infl*/
if infl in (-1 -2) then sinfl = -1; else if infl in (1 2) then sinfl=1;
if infl = -2 then infl = -1.5;else if infl = -1 then infl = -0.5;
if infl = 2 then infl = 1.5;else if infl = 1 then infl = 0.5;
/*mean pleasant ratings*/
mamp=mean(rrml, rvab, rgry);rvabrml = mean(rrml,rvab);
/*for regression*/
inflAMP = (infl)*AMP;cntrlAMP=cntrl*AMP;intentAMP=intent*AMP;seeFAMP=seeF*AMP;
inflampcntrl = inflAMP*cntrl; inflampintent = inflAMP*intent; 
inflcntrl = infl*cntrl; inflintent = infl*intent;
inflamp1 = (infl)*AMP1;cntrlamp1 = (cntrl)*AMP1;intentamp1 = (intent)*AMP1;
readAMP=read*AMP;
/*absolute*/
aexp = abs(ermlvab);
/*the strength of the manipulation*/
if index(formcnd, "9") > 0 then frm_strngth = 9;
else if index(formcnd, "12") > 0 then frm_strngth = 12;
else if index(formcnd, "16") > 0 then frm_strngth = 16;
if formcnd in ("r12" "r16") then do; ampPos = rrml-rvab; expPos = ermlvab; end;
else if formcnd in ("v12" "v16") then do; ampPos = rvab-rrml; expPos = ermlvab*-1; end;

/*9+12 vs. 16*/
if frm_strngth in (9 12) then strngth=0; else if frm_strngth=16 then strngth=1;
/*for regression*/
inflStrng = infl*frm_strngth; ampStrng = amp*frm_strngth; inflAmpStrng = inflamp*frm_strngth;

if minvr > 12 and mamp > 0 and mamp < 1 and read < 3 then ok=1; else ok=0; 
completed = amp ne . and intent ne . and ermlvab ne . and infl ne .;
include = ok and completed;

run;

proc freq data=allds; tables completed; run;
proc freq data=allds; tables include; where completed=1; run;

/*gender*/
proc freq data=allds; tables sex; where completed=1; run;
/*age*/
PROC MEANS DATA=allds n MEAN MAXDEC=2 fw=7 std stderr;VAR age;where completed=1; run;
/*reading Chinese*/
proc freq data=allds; tables mamp; where completed=1; run;
/*reading Chinese*/
proc freq data=allds; tables read; where completed=1; run;

/*no-diff vs. moderate-diff, explicit*/
PROC MEANS DATA=allds n MEAN MAXDEC=2 fw=7 std stderr;VAR aexp;class frm_strngth;where include=1; run;
proc ttest data=allds;class frm_strngth;var aexp;where frm_strngth in (9 12) and include=1; run;
proc ttest data=allds;class frm_strngth;var aexp;where frm_strngth in (12 16) and include=1; run;
/*AMP*/
PROC MEANS DATA=allds n MEAN MAXDEC=2 fw=7 std stderr;VAR amp_extrm aamp sdamp;class frm_strngth;where include=1; run;
proc ttest data=allds;class frm_strngth;var amp_extrm aamp sdamp;where frm_strngth in (9 12) and include=1; run;
proc ttest data=allds;class frm_strngth;var amp_extrm aamp sdamp;where frm_strngth in (12 16) and include=1; run;
proc ttest data=allds;class strngth;var amp_extrm aamp sdamp;where include=1; run;
/*induction on RPE and INTENT*/
PROC MEANS DATA=allds n MEAN MAXDEC=2 fw=7 std stderr;VAR infl intent;class frm_strngth;where include=1; run;
proc ttest data=allds;class frm_strngth;var infl intent;where frm_strngth in (9 12) and include=1; run;
proc ttest data=allds;class frm_strngth;var infl intent;where frm_strngth in (12 16) and include=1; run;

/*RPE*/
proc freq data=allds; tables infl; where include=1; run;
PROC MEANS DATA=allds n MEAN MAXDEC=2 fw=7 std stderr t prt;VAR ampPos;class infl;where include=1; run;
PROC MEANS DATA=allds n MEAN MAXDEC=2 fw=7 std stderr t prt;VAR expPos;class infl;where include=1; run;
PROC MEANS DATA=allds n MEAN MAXDEC=2 fw=7 std stderr t prt;VAR amp_extrm aamp sdamp;class infl;where include=1; run;
/*reported priming effect and internal consistency*/
PROC SORT DATA=allds; BY infl; RUN;
proc corr data=allds;  var amp1; with amp2; by infl; where include=1; run;
/*reported priming effect and AMP/explicit correlation*/
PROC SORT DATA=allds; BY infl; RUN;
proc corr data=allds;  var amp; with ermlvab; by infl; where include=1; run;

/*intent*/
proc freq data=allds; tables intent; where include=1; run;
PROC MEANS DATA=allds n MEAN MAXDEC=2 fw=7 std stderr t prt;VAR ampPos;class intent;where include=1; run;
PROC MEANS DATA=allds n MEAN MAXDEC=2 fw=7 std stderr t prt;VAR expPos;class intent;where include=1; run;
PROC MEANS DATA=allds n MEAN MAXDEC=2 fw=7 std stderr t prt;VAR amp_extrm aamp sdamp;class intent;where include=1; run;
/*intent and internal consistency*/
PROC SORT DATA=allds; BY intent; RUN;
proc corr data=allds;  var amp1; with amp2; by intent; where include=1; run;
/*intent and AMP/explicit correlation*/
PROC SORT DATA=allds; BY intent; RUN;
proc corr data=allds;  var amp; with ermlvab; by intent; where include=1; run;
